package redis;

import redis.clients.jedis.Jedis;

import java.util.Collections;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/10/16 17:18
 */

public class RedisLock {
    public static void main(String[] args) {
        Jedis jedis=new Jedis("192.168.0.27",6379);
        String res= jedis.set("2","1","NX","EX",10);
        if(res==null) {System.out.println("未获取到锁");return;}
        System.out.println("锁定数据");
        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
        Object result = jedis.eval(script, Collections.singletonList("2"), Collections.singletonList("1"));
        if(result.equals(1L)) System.out.println("解锁数据");
        System.out.println(result);
        jedis.close();
    }
}
